package com.atzxh.guigu.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 小红同学
 * @date 2023-05-08-13:57
 * @description     读数据
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws Exception {

        // 创建文件的输入流
        File file = new File("file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 通过输入流获取对应的file channel -->实际类型 FileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();

        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

        // 将通道的数据读入到buffer
        fileChannel.read(byteBuffer);

        // 将字节转成字符串
        System.out.println(new String(byteBuffer.array()));

        fileInputStream.close();
    }
}
